<?php
class Fabric_typeModel extends BaseModel
{
	var $table  = 'fabric_type';
	var $prikey = 'fabric_type_id';
	var $_name  = 'fabric_type';
	
	/**
	 * 取得档口类型列表
	 *
	 * @param int $parent_id 大于等于0表示取某个类型的下级类型，小于0表示取所有类型
	 * @return array
	 */
	function get_list($parent_id = -1)
	{
		if ($parent_id >= 0)
		{
			return $this->find(array(
					'conditions' => "parent_id = '$parent_id'",
					'order' => 'fabric_type_id',
			));
		}
		else
		{
			return $this->find(array(
					'order' => 'fabric_type_id',
			));
		}
	}
	
	
	/**
	 * 取得options，用于下拉列表
	 */
	function get_options($parent_id = 0)
	{
		$res = array();
		$fabrics = $this->get_list($parent_id);
		foreach ($fabrics as $fabric)
		{
			$res[$fabric['fabric_type_id']] = $fabric['fabric_type_name'];
		}
		return $res;
	}
	
	/*
	 * 判断名称是否唯一
	*/
	function unique($type_name, $parent_id, $type_id = 0)
	{
		$conditions = "parent_id = '" . $parent_id . "' AND fabric_type_name = '" . $type_name . "'";
		$type_id && $conditions .= " AND fabric_type_id <> '" . $type_id . "'";
		return count($this->find(array('conditions' => $conditions))) == 0;
	}
}